package com.app.utils.util;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/*
 * 时间工具类
 */
public class DateUtils {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd  EEEE");
    private static SimpleDateFormat sdf_hh_mm = new SimpleDateFormat("HH:mm");
    private static SimpleDateFormat sdf_yyyy_MM_dd = new SimpleDateFormat("yyyy.MM.dd");

    /*
     * 将时间戳转为字符串 ，格式：yyyy-MM-dd HH:mm
     */
    public static String getStrTime_ymd_hm(String cc_time) {
        String re_StrTime = "";
        if (TextUtils.isEmpty(cc_time) || "null".equals(cc_time)) {
            return re_StrTime;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        // 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;

    }

    /*
     * 将时间戳转为字符串 ，格式：yyyy-MM-dd HH:mm:ss
     */
    public static String getStrTime_ymd_hms(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;

    }

    /*
     * 将时间戳转为字符串 ，格式：yyyy.MM.dd
     */
    public static String getStrTime_ymd(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        // 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;
    }

    /*
     * 将时间戳转为字符串 ，格式：dd
     */
    public static String getStrTime_dd(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        // 例如：cc_time=1291778220
        try {
            long lcc_time = Long.valueOf(cc_time);
            re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return re_StrTime;
    }

    /*
     * 将时间戳转为字符串 ，格式：yyyy
     */
    public static String getStrTime_y(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        // 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;
    }

    /**
     * 将字符串转成时间戳
     * @param user_time
     * @return
     */
    public static String getTime(String user_time) {
        String re_time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d;
        try {
            d = sdf.parse(user_time);
            long l = d.getTime();
            String str = String.valueOf(l);
            re_time = str.substring(0, 10);
        }catch (ParseException e) {
            // TODO Auto-generated catch block e.printStackTrace();
        }
        return re_time;
    }

    /*
     * 将时间戳转为字符串 ，格式：MM-dd
     */
    public static String getStrTime_md(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        // 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;
    }

    /*
     * 将时间戳转为字符串 ，格式：MM-dd HH:mm  (我的评论)
     */
    public static String getStrTime_mdhm(String cc_time) {
        if (TextUtils.isEmpty(cc_time) || "null".equals(cc_time)) {
            return "";
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
        String currentTime = sdf.format(new Date(System.currentTimeMillis()));
        String[] currentTimes = currentTime.split("-");

        long lcc_time = Long.valueOf(cc_time);
        String re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        String[] reTimes = re_StrTime.split("-");

        if (currentTimes[0].equals(reTimes[0])) {

            if (currentTimes[1].equals(reTimes[1])) {

                if (currentTimes[2].equals(reTimes[2])) {
                    return "今天 " + reTimes[3] + ":" + reTimes[4];
                } else {
                    int currentDay = Integer.parseInt(currentTimes[2]);
                    int reDay = Integer.parseInt(reTimes[2]);
                    if (currentDay - reDay == 1) {
                        return "昨天" + reTimes[3] + ":" + reTimes[4];
                    } else {
                        return reTimes[1] + "-" + reTimes[2] + " " + reTimes[3] + ":" + reTimes[4];
                    }
                }

            } else {
                return reTimes[1] + "-" + reTimes[2] + " " + reTimes[3] + ":" + reTimes[4];
            }

        } else {
            return reTimes[1] + "-" + reTimes[2] + " " + reTimes[3] + ":" + reTimes[4];
        }
    }


    /*
     * 将时间戳转为字符串 ，格式：HH:mm
     */
    public static String getStrTime_hm(long lcc_time) {
        String re_StrTime = null;
        // 例如：cc_time=1291778220
        re_StrTime = sdf_hh_mm.format(new Date(lcc_time * 1000L));
        return re_StrTime;
    }

    /*
     * 将时间戳转为字符串 ，格式：HH:mm:ss
     */
    public static String getStrTime_hms(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        // 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;
    }

    /*
     * 将时间戳转为字符串 ，格式：MM-dd HH:mm:ss
     */
    public static String getNewsDetailsDate(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");
        // 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;
    }


    /**
     * 将当前时间转为字符串
     *
     * @param type 时间格式
     * @return
     */
    public static String getCurrentTime(String type) {
        long currentTime = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat(type);
        Date d = new Date(currentTime);
        String re_time = sdf.format(d);
        return re_time;
    }



    public static String afterNDay(String type,int n){
        Calendar c=Calendar.getInstance();
        SimpleDateFormat df=new SimpleDateFormat(type);
        c.setTime(new Date());
        c.add(Calendar.DATE,n);
        Date d2=c.getTime();
        String s=df.format(d2);
        return s;
    }



    /*
     * 将时间戳转为字符串 ，格式：yyyy.MM.dd  星期几
     */
    public static String getSection(String lcc_time) {
        String re_StrTime;
//		对于创建SimpleDateFormat传入的参数：EEEE代表星期，如“星期四”；MMMM代表中文月份，如“十一月”；MM代表月份，如“11”；
//		yyyy代表年份，如“2010”；dd代表天，如“25”
        // 例如：lcc_time=1291778220
        try {
            re_StrTime = sdf.format(new Date(Long.valueOf(lcc_time) * 1000L));
        } catch (Exception e) {
            re_StrTime = null;
            e.printStackTrace();
        }
        return re_StrTime;
    }

    /**
     * @param lcc_time
     * @return
     */
    public static String getSection2(String lcc_time) {
        if (TextUtils.isEmpty(lcc_time)) {
            return "";
        }
        long time;
        try {
            time = Long.valueOf(lcc_time);
        } catch (NumberFormatException e) {
            time = 0;
            e.printStackTrace();
        }
        return sdf_yyyy_MM_dd.format(new Date(time * 1000L));
    }

    /**
     *  将 yyyy/MM/dd HH:mm:ss 格式的时间,以天为单位转换成 毫秒值
     * @param simpleDate yyyy/MM/dd的事件
     * @return
     */
    public static long simpleDate2Millisecond(String simpleDate){
        long millisTime = System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        try {
            Date chooseDate = simpleDateFormat.parse(simpleDate);
            millisTime = chooseDate.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return millisTime;
    }





    public static String strSecond2StrMinute(String secondStr){
        if(TextUtils.isEmpty(secondStr)){
            return "";
        }

        try{
            int second = Integer.valueOf(secondStr);
//            long h=second/3600;
//            long m=second/60%60;
//            long s=second%60;
//            String format = "%02d:%02d:%02d";
//
//            String fs = String.format(format,h,m,s);
//            return fs;
            int h = 0;
            int m = 0;
            int s = 0;
            String hh = "";
            String mm = "";
            String ss = "";
            h=second/3600;
            m=(second-h*3600)/60;
            s=second-(h*3600)-(m*60);
            if(h<10){
                hh = "0"+h;
            }else{
                hh = h+"";
            }
            if(m<10){
                mm = "0"+m;
            }else{
                mm = m+"";
            }
            if(s<10){
                ss = "0"+s;
            }else{
                ss = s+"";
            }

            return hh+":"+mm+":"+ss;

        }catch(Exception e){
            return "";
        }


    }



}
